<?php
/**
 * DMFramework
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @url http://dmframework.nl
 */

/**
 * The layout class is always being used even if you don't have layouts enabled
 * this is because it saves code and we just create an empty layout when disabled.
 * It can render views.
 * 
 * @license Copyright 2012 Dillen Meijboom under the Apache license version 2.0 (http://apache.org/licenses/LICENSE-2.0)
 * @author Dillen Meijboom <info@dillenm.nl>
 * @package System.Core
 */
class Dmf_Layout extends Dmf_View
{
    /**
     * All views
     * 
     * @var array 
     */
    private $_views = array();
    
    /**
     * All CSS and Script files
     * 
     * @var array
     */
    private $_files = array();
    
    /**
     * The main view
     * 
     * @var Dmf_Resource
     */
    public $view;
    
    /**
     * Add a file
     * 
     * @param string $file
     * @param string $type 
     */
    public function addFile( $file, $type )
    {
        $this->_files[] = array(
            'filename' => $file,
            'type'     => $type
        );
    }
    
    /**
     * Clean all views 
     */
    public function clearViews()
    {
        $this->_views = array();
    }
    
    /**
     * Get head links
     *  
     * @return string
     */
    public function getHead()
    {
        $head = '';
        
        foreach( $this->_files as $file )
        {
            switch( $file['type'] )
            {
                case 'css':
                    $head .= '<link rel="stylesheet" type="text/css" href="' . $file['filename'] . '" />' . "\n";
                    break;
                case 'js':
                case 'javascript':
                    $head .= '<script type="text/javascript" scr="' . $file['filename'] . '"></scripts>' . "\n";
            }
        }
        
        return $head;
    }
    
    /**
     * Add a view to the view collection
     * 
     * @param Dmf_Resource $view 
     */
    public function addView( Dmf_Resource $view )
    {
        $this->_views[] = $view;
        
        if ( ! $this->view instanceof Dmf_Resource )
        {
            $this->view = $this->_views['0'];
        }
    }
    
    /**
     * Import ini file to view and layout memory
     * 
     * @param string $file
     * @param string $name
     * @throws Dmf_Exception_FileNotFound 
     */
    public function import( $file, $name )
    {
        if ( ! strstr( $file, '/' ) )
        {
            $file = 'application/configs/' . $file;
        }
        
        if ( ! file_exists( $file ) ) 
        {
            throw new Dmf_Exception_FileNotFound( 'Could not import data for views because file: "' . $file . '" does not exist.' );
        }
        
        $memory = new Dmf_Config_Ini( $file );
        
        parent::import( $memory->asArray(), $name );
        $this->view->import( $memory->asArray(), $name );
    }
}